package com.abel.bigwater.dataserver.model.ewide

import com.abel.bigwater.data.BcdString
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.serializer.SerializerFeature
import io.netty.buffer.Unpooled
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Test
import org.slf4j.LoggerFactory

class KmingResponseDataTest {

    @Test
    fun parseBuf() {
        val buf = Unpooled.buffer().apply { writeBytes(BcdString.toByteArray("8100")) }
        val d = KmingResponseData(KmingDataType.TermSendDataReply)
        assert(d.parseBuf(buf))

        assertEquals(SessionReponseType.Success, d.sessionReponseType)
    }

    @Test
    fun pack() {
        val d = KmingResponseData(KmingDataType.TermSendDataReply).apply {
            sessionReponseType = SessionReponseType.Success
        }

        val buf = Unpooled.buffer().apply { d.pack(this) }

        assertEquals(2, buf.readableBytes())
        assertEquals("8100", BcdString.hexString(buf))
    }

    @Test
    fun packWhole() {
        val p = KmingPacket().apply {
            termCode = TermCodeType().also {
                it.manuId = BcdString.fromBcdString("03")
                it.codeBcd = BcdString.fromBcdString("01000000")
            }

            deviceStatus = DeviceStatus().also {
                it.lowBattery10 = true
                it.magnet21 = true
                it.breakLine30 = true
            }

            frameNo = 1
            data = KmingResponseData(KmingDataType.TermSendDataReply).apply {
                sessionReponseType = SessionReponseType.Success
            }
        }

        val buf = Unpooled.buffer().apply {
            p.pack(this)

            assertEquals("5A550100000003010201010002008100A77F6A69", BcdString.hexString(this).toUpperCase())
        }
    }

    @Test
    fun parseWhole() {
        val buf = Unpooled.buffer().apply { writeBytes(BcdString.toByteArray("5A550100000003010201010002008100A77F6A69")) }
        val p = KmingPacket()
        Assert.assertTrue(p.parseBuf(buf))

        lgr.info(JSON.toJSONStringWithDateFormat(p, JSON.DEFFAULT_DATE_FORMAT, SerializerFeature.PrettyFormat))

        assertEquals(TermCodeType().apply {
            manuId = BcdString.fromBcdString("03")
            codeBcd = BcdString.fromBcdString("01000000")
        }, p.termCode)

        val data = p.data as KmingResponseData
        assertEquals(SessionReponseType.Success, data.sessionReponseType)
    }

    @Test
    fun parseBufBurst() {
        val buf = Unpooled.buffer().apply { writeBytes(BcdString.toByteArray("8200")) }
        val d = KmingResponseData(KmingDataType.TermBurst)
        assert(d.parseBuf(buf))

        assertEquals(SessionReponseType.Success, d.sessionReponseType)
    }

    @Test
    fun packBurst() {
        val d = KmingResponseData(KmingDataType.TermBurst).apply {
            sessionReponseType = SessionReponseType.Success
        }

        val buf = Unpooled.buffer().apply { d.pack(this) }

        assertEquals(2, buf.readableBytes())
        assertEquals("8200", BcdString.hexString(buf))
    }

    @Test
    fun packWholeBurst() {
        val p = KmingPacket().apply {
            termCode = TermCodeType().also {
                it.manuId = BcdString.fromBcdString("03")
                it.codeBcd = BcdString.fromBcdString("01000000")
            }

            deviceStatus = DeviceStatus().also {
                it.lowBattery10 = true
                it.magnet21 = true
                it.breakLine30 = true
            }

            frameNo = 1
            data = KmingResponseData(KmingDataType.TermBurst).apply {
                sessionReponseType = SessionReponseType.Success
            }
        }

        val buf = Unpooled.buffer().apply {
            p.pack(this)

            assertEquals("5A550100000003010201010002008200A78F6A69", BcdString.hexString(this).toUpperCase())
        }
    }

    @Test
    fun parseWholeBurst() {
        val buf = Unpooled.buffer().apply { writeBytes(BcdString.toByteArray("5A550100000003010201010002008200A78F6A69")) }
        val p = KmingPacket()
        Assert.assertTrue(p.parseBuf(buf))

        lgr.info(JSON.toJSONStringWithDateFormat(p, JSON.DEFFAULT_DATE_FORMAT, SerializerFeature.PrettyFormat))

        assertEquals(TermCodeType().apply {
            manuId = BcdString.fromBcdString("03")
            codeBcd = BcdString.fromBcdString("01000000")
        }, p.termCode)

        val data = p.data as KmingResponseData
        assertEquals(KmingDataType.TermBurst, data.type)
        assertEquals(SessionReponseType.Success, data.sessionReponseType)
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(KmingResponseDataTest::class.java)
    }
}